package SpaceIT.pages;

import java.util.ArrayList;
import model.dao.*;
import model.pojo.*;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import view.util.util;

public class TimKiem 
{
	@Inject
	private Request request;
	
	@Property
	private util viewHelper ;
	
	private ArrayList<SanPham> arr;//san pham
	
	@Property
	private int i;
	
	@Property
	private int j;
	
	@Property
	private int Option;
	
	@Property
	private ArrayList<ArrayList<String>> arr1 ;
	
	@Property
	private ArrayList<HangSanXuat> arr2;//hang san xuat
	
	@Property
	private ArrayList<DongSanPham> arr3;//DongSanPham

	@Property
	private ArrayList<String> sp;
	
	private String key;
	
	public TimKiem() {
//	{
		viewHelper = new util();
	}
	
	void onActivate() 
	{
		String s = request.getParameter("TimKiem");
		if(s != null)
		{
			if(s.contains("'"))
				s = s.replaceAll("'", "\\\\'");
			arr1 = SanPhamDAO.TimKiem("%" + s + "%");
			SanPhamTimKiem();
			return;
		}
		s = request.getParameter("ac");
		if(s != null)
		{
			if(s.equals("0"))
			{
				String op = request.getParameter("op");
				String giaTu = request.getParameter("giaTu");
				String giaDen = request.getParameter("giaDen");
				arr1 = SanPhamDAO.LaySanPham(0, op , giaTu , giaDen);
				SanPhamTimKiem();
			}
			if(s.equals("1"))
			{
				String cpu = request.getParameter("cpu");
				arr1 = SanPhamDAO.LaySanPham(1, cpu , "" , "");
				SanPhamTimKiem();
			}
			if(s.equals("2"))
			{
				String ram = request.getParameter("ram");
				arr1 = SanPhamDAO.LaySanPham(2, ram , "" , "");
				SanPhamTimKiem();
			}
			if(s.equals("3"))
			{
				String hdd = request.getParameter("hdd");
				arr1 = SanPhamDAO.LaySanPham(3, hdd , "" , "");
				SanPhamTimKiem();
			}
			if(s.equals("4"))
			{
				String cell = request.getParameter("cell");
				arr1 = SanPhamDAO.LaySanPham(4, cell , "" , "");
				SanPhamTimKiem();
			}
		}
	}

	public void setKey(String key){
//	{
		this.key = key;
	}
	
	public String getKey(){
//	{
		return key;
	}
    
	public int SoLuongNhaSanXuat(){
//	{
		arr2 = NhaSanXuatDAO.HangSanXuatSanPham("%Laptop%");
		if(arr2 == null)
			return 0;
		return arr2.size();
	}
	
	public int SoLuongDongSanPham(int MaHangSX){
//	{
		arr3 = DongSanPhamDAO.DongSanPhamTheoNSX(1, MaHangSX);
		if(arr3 == null)
			return 0;
		return arr3.size();
	}
	
	public String getTenNhaSanXuat(int i){
//	{
		return arr2.get(i).getTenHangSX();
	}
	
	public String getMaNhaSanXuat(int i){
//	{
		return arr2.get(i).getMaHangSX();
	}
	
	public String getTenDongSanPham(int i){
//	{
		return arr3.get(i).getMoTa();
	}
		
	public String getMaDongSanPham(int i){
//	{
		return String.valueOf(arr3.get(i).getMaDongSP());
	}
	
	public boolean isOutPut(){
//	{
		if (i < sp.size())
			return true;
		return false;
	}
	
	public boolean Arr1IsNull(){
//	{
		if(arr1 == null)
			return true;
		return false;
	}
	
	private void SanPhamTimKiem() {
//	{
		if(arr1 == null)
			return ;
		
		sp = new ArrayList<String>();
		
		for(int i = 0 ; i < arr1.size() ; i = i + 10)
		{
			String s = "";
			for(int j = 0 ; j < 10 && i + j < arr1.size() ; j++ )
			{
				s += "<div class='products_list_content_item'>";
				s += "	<div class='products_list_content_item_pic'>";
				s += "		<a href='" + arr1.get(i + j).get(8) + "?MaSP=" + arr1.get(i + j).get(0) + "' title='" + arr1.get(i + j).get(1) +"'>";
				s += "			<img src='" + arr1.get(i + j).get(7) + "' alt='" + arr1.get(i + j).get(1) +" width='120px' height='120px'/>";
				s += "		</a>";
				s += "		<p><br/>" + arr1.get(i + j).get(2) + " VNĐ</p>";
				s += "		<div class='luotxem'> Lượt xem: " + arr1.get(i + j).get(6) + "</div>";
				s += "	</div>";
				s += "	<div class='products_list_content_item_main'>";
				s += "		<div class='products_list_content_item_name'>";
				s += "			<a href='" + arr1.get(i + j).get(8) + "?MaSP=" + arr1.get(i + j).get(0) + "' title='" + arr1.get(i + j).get(1) +"'>";
				s +=  				arr1.get(i + j).get(1);
				s += "			</a>";
				s += "		</div>";
				s += "		<div class='products_list_content_item_des'>";
				s += "			<span style='font-size: small; font-family: arial,helvetica,sans-serif;'>";
				s += 				arr1.get(i + j).get(3);	
				s += "			</span>"; 
				s += "			<br/>";
				s += "		</div>";
				s += "		<div class='products_list_content_item_warr'>";
				s += "			<span class='warr_padd' style='padding-left: 20px;'>Bảo hành: " + arr1.get(i + j).get(4) + " Tháng</span>";
				s += "			<span class='warr_padd'> Kho: " + arr1.get(i + j).get(5) + "</span>";
				s += String.format("<input type='image' style='padding-right: 200px; padding-top: 9px; float: right;' src='layout/images/GiaoDien/dathang.gif' url='%s' MaSP='%s' TenSP='%s' GiaBan='%s' class='btnMua' />", arr1.get(i + j).get(8) + "?MaSP=" , arr1.get(i + j).get(0) , arr1.get(i + j).get(1) , arr1.get(i + j).get(2));
				s += "			<div style='clear: both; margin-bottom: 3px;'></div>";
				s += "		</div>";
				s += "	</div>";
				s += "	<div class='clr products_list_content_bot'></div>";
				s += "</div>";
			}
			sp.add(s);
		}
	}
}
